package com.pinc.poop;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;

public class PoopCalendar extends PincCalendarView {
	public int[][] times;
	private DBClass dbHelper;

	public PoopCalendar(Context context, AttributeSet attrs) {
		super(context, attrs);
		times = new int[6][7];
		dbHelper = new DBClass(context);
	}

	@Override
	protected int getLayoutResource() {
		return R.layout.calendar;
	}

	@Override
	protected String getGridIdName() {
		return "dg";
	}

	@Override
	protected OnClickListener getClickListener(PincDate date) {
		return new mClickListener(mContext, date.toDateStr());
	}

	private class mClickListener implements View.OnClickListener
	{
		private String date_str;
		private Context mContext;

		public mClickListener(Context context, String date_str) {
			this.date_str = date_str;
			mContext = context;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, daily_poop.class);
			Bundle bd = new Bundle();
			bd.putString("dd", date_str);
			intent.putExtras(bd);
			mContext.startActivity(intent);
		}
	}

	@Override
	public void prepareData() {
		PincDate l_date = new PincDate(mDate);

		l_date.go2FirstDayofMonth().go2FirstDayofWeek();

		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if ( l_date.month == mDate.month)
				{
					Cursor cs = dbHelper.getRecord("date = ?", new String[]{l_date.toDateStr()}, null);
					times[i][j] = cs.getCount();
					cs.close();
				}
				else
					times[i][j] = 0;
				l_date.nextDay(1);
			}
		}
		dbHelper.close();
	}

	@Override
	public void updateData() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				PincCalendarPoopGridView grid_view = (PincCalendarPoopGridView)mGridView[i][j];
				grid_view.updateDataView(times[i][j]);
			}
		}
	}

	@Override
	public void clearData() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				PincCalendarPoopGridView grid_view = (PincCalendarPoopGridView)mGridView[i][j];
				grid_view.clearDataView();
			}
		}
	}

}
